/*
 * Licensed to Nanjing RedOrange Co. ltd (RedOrange) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * RedOrange licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package redora.rdo.api;

import java.util.LinkedList;
import java.util.List;

/**
 * The BusinessRuleViolation is a holding class that wraps up all the necessary
 * details when a business rule is violated. All business rules must be numbered
 * incrementally within the scope of the persistence class with is defined in
 * the model file. With this id, the BusinessRuleHandler can locate the
 * appropriate message belonging to the violated rule. There are also standard
 * business rules (see StandardRule enum), you might want to look at these rules
 * when you create your own custom rules, it can save you a lot of trouble
 * translating every time the same texts when you use the StandardRules.
 * 
 * @author jaap
 */
public class BusinessRuleViolation {
	/**
	 * Standard (usually generated) business rules, available for all projects
	 * using this library.
	 */
	public enum StandardRule {
		/** Check if the attribute has null value */
		NotNull(1),
		/** Check if the attribute's length is bigger than maxlength */
		MaxLength(2),
		/**
		 * Check if the attribute's value match the "regexp" string defined in
		 * model
		 */
		MatchRegexp(3),
		/** Check if the xml attribute value passes the xsd checking */
		XsdValidation(4),
		/** Wrapper for unique key exception from the database. */
		UniqueKey(5);

		public Integer ruleId;

		private StandardRule(Integer ruleId) {
			this.ruleId = ruleId;
		}
	}

	/** CrUD Action */
	public enum Action {
		Insert, Update, Delete;
	}

	public BusinessRuleViolation(Persistable persistable,
			Enum<?> field, Integer businessRuleId, Action action) {
		super();
		this.persistable = persistable;
		this.field = field;
		this.businessRuleId = businessRuleId;
		this.action = action;
	}

	public BusinessRuleViolation(Persistable persistable,
			Enum<?> field, Integer businessRuleId, Action action,
			List<Object> messageArguments) {
		super();
		this.persistable = persistable;
		this.field = field;
		this.businessRuleId = businessRuleId;
		this.action = action;
		this.messageArguments = messageArguments;
	}

	/** Pojo class where on the BR was violated. */
	private Persistable persistable;

	/** Pojo field name for violation, see the repective 'Pojo'Field */
	private Enum<?> field;

	/** Every business rule has an unique identifier */
	private Integer businessRuleId;

	/** Messages(optional) for violation. */
	private List<Object> messageArguments = new LinkedList<Object>();

	/** Action for business rule violated (Insert or Delete or Update) */
	private Action action;

	/** Indicates at what (CrUD) Action is the business rule violated. */
	public Action getAction() {
		return action;
	}

	public void setAction(Action action) {
		this.action = action;
	}

	public void setMessageArguments(List<Object> messageArguments) {
		this.messageArguments = messageArguments;
	}

	public Persistable getPersistable() {
		return persistable;
	}

	public void setPersistable(Persistable persistable) {
		this.persistable = persistable;
	}

	public Enum<?> getField() {
		return field;
	}

	public void setField(Enum<?> field) {
		this.field = field;
	}

	public Integer getBusinessRuleId() {
		return businessRuleId;
	}

	public void setBusinessRuleId(Integer businessRuleId) {
		this.businessRuleId = businessRuleId;
	}

	public List<Object> getMessageArguments() {
		return messageArguments;
	}
}
